﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Chaine.DAO;
using Chaine.Models;
using System.Text.RegularExpressions;
using System.IO;
using MongoDB.Bson;

namespace Chaine.Controllers
{
    public class AdminEventController : Controller
    {
        //
        // GET: /AdminEvent/

        public ActionResult Index()
        {
            var events = EventManager.GetEvents();
            return View("~/views/admin/event/index.cshtml", events);
        }

        //
        // GET: /AdminEvent/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /AdminEvent/Create

        public ActionResult Create()
        {
            return View("~/views/admin/event/create.cshtml");
        }

        //
        // POST: /AdminEvent/Create

        [HttpPost]
        public ActionResult Create(EventModels e, FormCollection collection, HttpPostedFileBase file)
        {
            try
            {
                ModelState.Clear();
                //判断上传的文件是否为空的文件
                if (file != null && file.ContentLength == 0)
                    ModelState.AddModelError("poster", "Your filed is empty.");
                //判断上传文件的后缀名
                if (file != null && !Regex.IsMatch(file.FileName.ToLower(), @"^*\.(jpg|jpeg|png)$"))
                    ModelState.AddModelError("poster", "File extention is wrong, please upload JPG, JEPG or PNG file.");
                if (string.IsNullOrEmpty(e.Title))
                    ModelState.AddModelError("title", "Event title is required.");
                //如果表单内容不符合则不继续提交
                if (!ModelState.IsValid)
                    return View("~/views/admin/event/create.cshtml");


                //上传照片
                if (file != null && file.ContentLength > 0)
                {
                    var match = Regex.Match(file.FileName.ToLower(), @"\.(jpg|jpeg|png)$");
                    var extension = match.ToString();//文件后缀名
                    var filename = Guid.NewGuid().ToString() + extension;
                    var folder = HttpContext.Server.MapPath("/poster");
                    if (!Directory.Exists(folder))
                        Directory.CreateDirectory(folder);
                    var filePath = Path.Combine(folder, filename);
                    file.SaveAs(filePath);
                    e.Poster = filename;
                }

                e.IsDeleted = false;

                //保存Event对象
                EventManager.CreateEvent(e);
                TempData["create-success"] = "";
                return RedirectToAction("Index");
            }
            catch
            {
                return View("~/views/admin/event/create.cshtml");
            }
        }

        //
        // GET: /AdminEvent/Edit/5

        public ActionResult Edit(string id)
        {
            var e = EventManager.GetEvent(ObjectId.Parse(id));
            return View("~/views/admin/event/edit.cshtml", e);
        }

        //
        // POST: /AdminEvent/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, EventModels e, FormCollection collection, HttpPostedFileBase file)
        {
            try
            {
                e.Id = ObjectId.Parse(id);
                var old = EventManager.GetEvent(e.Id);
                e.Poster = old.Poster;
                ModelState.Clear();
                //判断上传的文件是否为空的文件
                if (file != null && file.ContentLength == 0)
                    ModelState.AddModelError("poster", "Your filed is empty.");
                //判断上传文件的后缀名
                if (file != null && !Regex.IsMatch(file.FileName.ToLower(), @"^*\.(jpg|jpeg|png)$"))
                    ModelState.AddModelError("poster", "File extention is wrong, please upload JPG, JEPG or PNG file.");
                if (string.IsNullOrEmpty(e.Title))
                    ModelState.AddModelError("title", "Event title is required.");
                //如果表单内容不符合则不继续提交
                if (!ModelState.IsValid)
                    return View("~/views/admin/event/edit.cshtml", e);


                //上传照片
                if (file != null && file.ContentLength > 0)
                {
                    var match = Regex.Match(file.FileName.ToLower(), @"\.(jpg|jpeg|png)$");
                    var extension = match.ToString();//文件后缀名
                    var filename = Guid.NewGuid().ToString() + extension;
                    var folder = HttpContext.Server.MapPath("/poster");
                    if (!Directory.Exists(folder))
                        Directory.CreateDirectory(folder);
                    var filePath = Path.Combine(folder, filename);
                    file.SaveAs(filePath);
                    e.Poster = filename;
                }

                //保存Event对象
                EventManager.UpdateEvent(e);
                TempData["update-success"] = "";
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminEvent/Delete/5

        public ActionResult Delete(string id)
        {
            var e = EventManager.GetEvent(ObjectId.Parse(id));
            return View("~/views/admin/event/delete.cshtml", e);
        }

        //
        // POST: /AdminEvent/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, FormCollection collection)
        {
            try
            {
                EventManager.DeleteEvent(ObjectId.Parse(id));
                TempData["delete-success"] = "";
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
